using Quartz;

namespace Zhaoxi.Zhihu.UseCases.Questions.Job;

public static class UpdateQuestionViewCountJobSchedule
{
    private static readonly TriggerKey Key = new(nameof
        (UpdateQuestionViewCountJobSchedule), nameof(Questions));

    public static void CreateUpdateQuestionViewCountJobSchedule(
        this IServiceCollectionQuartzConfigurator configurator,
        int intervalTime = 20)
    {
        configurator.AddJob<UpdateQuestionViewCountsJob>(triggerConfigurator =>
            triggerConfigurator
                .WithIdentity(UpdateQuestionViewCountsJob.Key));

        configurator.AddTrigger(trigger => trigger
            .WithIdentity(Key)
            .ForJob(UpdateQuestionViewCountsJob.Key)
            .StartAt(DateBuilder.FutureDate(intervalTime, IntervalUnit.Second))
            .WithSimpleSchedule(builder => builder
                .RepeatForever()
                .WithInterval(TimeSpan.FromSeconds(intervalTime))
            ));
    }
}